package net.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import net.mapper.UserMapper;
import net.pojo.Artile;
import net.pojo.Comments;
import net.pojo.Dicuss;
import net.pojo.User;
import net.service.ArtileService;
import net.service.CommentsService;
import net.service.LinksService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * @Author admin
 * @Date 2019/5/22 10:24
 */
@Controller
@RequestMapping("/artile")
public class ArtileController {
    @Autowired
    ArtileService artileService;
    @Autowired
    CommentsService commentsService;
    @Autowired
    LinksService linksService;
    @Autowired
    UserMapper userMapper;

    @RequestMapping("/findAll")
    public String findAll(Integer currentPage,Model model,HttpSession session){
        //分页
        PageHelper.startPage(currentPage, 4);
        List<Artile> list = artileService.findAll();
        PageInfo info = new PageInfo(list);
        //根据评论量排行
        List<Artile> commentList = artileService.findByComment();
        //根据点击量排行
        List<Artile> clickList = artileService.findByClick();
        //最新评论
        List<Comments> commentssList = commentsService.findByCtime();
        model.addAttribute("list",list);
        model.addAttribute("info",info);
        model.addAttribute("commentList",commentList);
        model.addAttribute("clickList",clickList);
        model.addAttribute("commentssList",commentssList);
        session.setAttribute("result",1);
        return "/client/index.jsp";
    }

    @RequestMapping("/findById")
    public String findById(Integer pid, Model model,HttpSession session){
        User user = (User) session.getAttribute("user");
        if (user == null){
            session.setAttribute("pid",pid);
            Artile artile = artileService.findById(pid);
            //根据点击量排行
            List<Artile> clickList = artileService.findByClick();
            //最新评论
            List<Comments> commentssList = commentsService.findByCtime();
            //相关推荐
            List<Artile> threeByTags = artileService.findThreeByTags(artile.getTags());
            Artile idPrev = artileService.findByIdPrev(pid);
            Artile idNext = artileService.findByIdNext(pid);
            artileService.updateClick(artile);
            List<Dicuss> dicussList = artileService.findAllByDpid(pid);
            for (Dicuss dicuss : dicussList){
                User user1 = userMapper.findById(dicuss.getDuid());
                dicuss.setUser(user1);
            }
            model.addAttribute("dicussList",dicussList);
            model.addAttribute("artile",artile);
            model.addAttribute("clickList",clickList);
            model.addAttribute("clickNum",artile.getClick());
            model.addAttribute("commentNum",artile.getComment());
            model.addAttribute("idPrev",idPrev);
            model.addAttribute("idNext",idNext);
            model.addAttribute("commentssList",commentssList);
            model.addAttribute("threeByTags",threeByTags);
            session.setAttribute("result",6);
            return "/client/content.jsp";
        }else {

            Artile artile = artileService.findById(pid);
            //根据点击量排行
            List<Artile> clickList = artileService.findByClick();
            //最新评论
            List<Comments> commentssList = commentsService.findByCtime();
            Artile idPrev = artileService.findByIdPrev(pid);
            Artile idNext = artileService.findByIdNext(pid);
            List<Artile> threeByTags = artileService.findThreeByTags(artile.getTags());
            List<Dicuss> dicussList = artileService.findAllByDpid(pid);
            for (Dicuss dicuss : dicussList){
                User user1 = userMapper.findById(dicuss.getDuid());
                dicuss.setUser(user1);
            }
            artileService.updateClick(artile);
            model.addAttribute("dicussList",dicussList);
            model.addAttribute("artile",artile);
            model.addAttribute("clickList",clickList);
            model.addAttribute("clickNum",artile.getClick());
            model.addAttribute("commentNum",artile.getComment());
            model.addAttribute("idPrev",idPrev);
            model.addAttribute("idNext",idNext);
            model.addAttribute("commentssList",commentssList);
            model.addAttribute("threeByTags",threeByTags);
            return "/client/content.jsp";
        }
    }

    @RequestMapping("/insertDicuss")
    public String insertDicuss(Integer pid,Dicuss dicuss,Model model,HttpSession session){
        User user = (User) session.getAttribute("user");

        Artile artile = artileService.findById(pid);
        //根据点击量排行
        List<Artile> clickList = artileService.findByClick();
        //最新评论
        List<Comments> commentssList = commentsService.findByCtime();
        List<Artile> threeByTags = artileService.findThreeByTags(artile.getTags());
        Artile idPrev = artileService.findByIdPrev(pid);
        Artile idNext = artileService.findByIdNext(pid);
        dicuss.setDpid(pid);
        dicuss.setDtime(new Date());
        dicuss.setDuid(user.getUid());
        artileService.insertDicuss(dicuss);
        List<Dicuss> dicussList = artileService.findAllByDpid(dicuss.getDpid());
        for (Dicuss dicuss2 : dicussList){
            User user1 = userMapper.findById(dicuss2.getDuid());
            dicuss2.setUser(user1);
        }
        artileService.updateComment(artile);
        model.addAttribute("dicussList",dicussList);
        model.addAttribute("artile",artile);
        model.addAttribute("clickList",clickList);
        model.addAttribute("clickNum",artile.getClick());
        model.addAttribute("commentNum",artile.getComment());
        model.addAttribute("idPrev",idPrev);
        model.addAttribute("idNext",idNext);
        model.addAttribute("commentssList",commentssList);
        model.addAttribute("threeByTags",threeByTags);
        return "/client/content.jsp";
    }

    @RequestMapping("/search")
    public String search(Integer currentPage,Model model,String search){
        //分页
        PageHelper.startPage(currentPage, 4);
        List<Artile> searchList = artileService.search(search);
        PageInfo pageInfo = new PageInfo(searchList);
        List<Artile> clickList = artileService.findByClick();
        List<Comments> commentssList = commentsService.findByCtime();
        model.addAttribute("searchList",searchList);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("clickList",clickList);
        model.addAttribute("search",search);
        model.addAttribute("commentssList",commentssList);
        return "/client/search.jsp";
    }

    @RequestMapping("/findByTags")
    public String findByTags(Integer currentPage,String tags,Model model){
        //分页
        PageHelper.startPage(currentPage, 4);
        List<Artile> tagsList = artileService.findByTags(tags);
        PageInfo pageInfo = new PageInfo(tagsList);
        List<Artile> clickList = artileService.findByClick();
        //最新评论
        List<Comments> commentssList = commentsService.findByCtime();
        model.addAttribute("tagsList",tagsList);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("clickList",clickList);
        model.addAttribute("commentssList",commentssList);
        model.addAttribute("tags",tags);
        return "/client/tags.jsp";
    }
}
